// mircoApp/cloud_shop/Invitation_code/Invitation_code.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    active: "0",
    checked: false,
    result: [],
    // 生成邀请码
    generate: false,
    // 生成提示
    generate_tips: false,
    showCode: false,
    generateCode: "",
    // 转让邀请码
    transferenceCode: false,
    transference_ID: "",
    info: [],
    codeList: [],
    page: 1,
    total_page: 0,
    isLoadMore: true,
    tMember: null,
    showCheckOption: false,
    code_url: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    this.init();
    this.getData();
    let member_center = wx.getStorageSync("yz_basic_info").lang.member_center;
    this.setData({
      credit1: member_center.credit1
    })
  },
  init() {
    this.setData({
      codeList: [],
      page: 1,
      total_page: 0,
    });
  },
  generate_tips_show() {
    this.setData({
      generate_tips: true,
    });
  },
  allChange(e) {
    console.log(e);
    let list = this.data.list;
    for (let i = 0; i < list.length; i++) {
      let check = this.selectComponent(`.checkboxGroup-${i}`);
      check.toggle();
    }
    this.setData({
      checked: e.detail,
    });
  },
  tapList(e) {
    let item = e.currentTarget.dataset.item;
    console.log(item,this.data.result.indexOf(String(item)),this.data.result)
    if (this.data.result.indexOf(String(item)) > -1) {
      this.data.result.splice(this.data.result.indexOf(String(item)), 1);
      this.setData({
        result: this.data.result,
      });
      return;
    }
    this.data.result.push(String(item));
    this.setData({
      result: this.data.result,
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
  async transferenceCode_confirm() {
    if (!this.data.transference_ID) {
      return app.tips("请输入转让邀请码会员");
    }
    if (this.data.result.length <= 0) {
      return app.tips("请选择转让邀请码");
    }
    let ids = this.data.result.map((item) => item);
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.cloud-shop.frontend.code.exchange"),
      data: {
        exchange_uid: this.data.transference_ID,
        exchange_ids: ids
      },
    });
    if (data.result) {
      app.tips(data.msg);
      this.initPopupClose();
    } else {
      app.tips(data.msg);
    }
  },
  async transferenceCode_inp() {
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.cloud-shop.frontend.code.getMember"),
      data: {
        uid: this.data.transference_ID
      },
    });
    if (data.result) {
      this.setData({
        tMember: data.data,
      });
    } else {
      this.setData({
        tMember: null,
      });
      app.tips(data.msg);
    }
  },
  tabChange(e) {
    let name = e.detail.name;
    this.setData({
      active: name
    })
    this.init();
    this.getData();
  },
  generateConfirm() {
    if (this.data.result.length == 0) {
      app.tips("请选择要转让的邀请码");
      return;
    }
    this.setData({
      transferenceCode: true,
    });
  },
  async generateCreatOrder() {
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.cloud-shop.frontend.create"),
      data: {
        code_num: this.data.generateCode
      },
    });
    if (data.result) {
      wx.navigateTo({
        url: `/packageD/buy/orderPay/orderPay?order_id=${data.data.order_ids}&status=2`,
      });
      this.initPopupClose();
    } else {
      this.initPopupClose();
      app.tips(data.msg);
    }
  },
  generate_show() {
    this.setData({
      generate: true,
    });
  },
  generateOpen() {
    if (this.data.codeList.length <= 0) {
      return;
    }
    this.setData({
      showCheckOption: true,
    });
    this.showCheckOption = true;
  },
  init() {
    this.setData({
      codeList: [],
      page: 1,
      total_page: 0,
    });
  },
  initPopupClose() {
    this.setData({
      showCheckOption: false,
      result: [],
      checked: false,
      tMember: false,
      generate: false,
      transferenceCode: false,
      showCode: false,
      generate_tips: false,
      generateCode: "",
      showCheckOption: false,
    });
    this.getData(true);
  },
  async showCodeOpen(e) {
    let {
      id
    } = e.currentTarget.dataset;
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.cloud-shop.frontend.code.getCodeImage"),
      data: {
        id: id
      },
    });
    if (data.result) {
      this.setData({
        code_url: data.data.code_url,
        showCode: true,
      });
    } else {
      app.tips(data.msg);
    }
  },
  async getData(key = false) {
    let json = {};
    json.use_type = this.data.active
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.cloud-shop.frontend.code.index"),
      data: json,
    });
    if (data.result) {
      this.setData({
        info: data.data,
        total_page: data.data.last_page,
      });
      if (!key) {
        this.setData({
          codeList: data.data.data,
        });
      }
      console.log(this.data.codeList, 'dddddddddddddd')
    } else {
      app.tips(data.msg);
    }
  },
  copuM(e) {
    let {
      info
    } = e.currentTarget.dataset;
    wx.setClipboardData({
      data: info,
      success(res) {
        app.tips("复制成功");
      },
    });
  },
  async toRecord() {
    wx.navigateTo({
      url: '/mircoApp/cloud_shop/Invitation_code_list/Invitation_code_list',
    })
  },
  async getMoreData() {
    if (!this.data.isLoadMore) return;
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.setData({
        page: this.data.page + 1,
      });
      let json = {
        page: this.data.page,
      };
      json.use_type = this.data.active
      let {
        data
      } = await app._postNetWork({
        url: app.getNetAddresss("plugin.cloud-shop.frontend.code.index"),
        data: json,
      });
      this.data.isLoadMore = true;
      if (data.result) {
        this.setData({
          codeList: this.data.codeList.concat(data.data.data),
        });
      } else {
        this.setData({
          page: this.data.page - 1,
        });
        app.tips(data.msg);
      }
    }
  },
});